package cn.wangzg.course0602;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.wangzg.course0602.util.HttpUtil;

/**
 * Time: 2020/3/31
 * Author: wangzhiguo
 * Description: 功能描述
 */
public class ArticleAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Article> articles;

    public ArticleAdapter(Context context) {
        this.context = context;
    }

    public ArticleAdapter(Context context, ArrayList<Article> articles) {
        this.context = context;
        this.articles = articles;
    }

    public void setArticles(ArrayList<Article> articles) {
        this.articles = articles;
        notifyDataSetChanged();//刷新
    }

    @Override
    public int getCount() {
        return articles==null?0:articles.size();
    }

    @Override
    public Object getItem(int position) {
        return articles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(context).inflate(
                R.layout.item_article,parent,false);
        TextView tvTitle=convertView.findViewById(R.id.textView);
        TextView tvUser=convertView.findViewById(R.id.textView3);
        TextView tvChapter=convertView.findViewById(R.id.textView5);
        TextView tvDate=convertView.findViewById(R.id.textView7);
        Article article=articles.get(position);
        tvTitle.setText(article.getTitle());
        tvUser.setText(article.getShareUser());
        tvChapter.setText(article.getSuperChapterName()+"/"+article.getChapterName());
        tvDate.setText(article.getNiceDate());
        ImageView ivCollect=convertView.findViewById(R.id.imageView);
        ivCollect.setSelected(article.isCollect());
        ivCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                article.setCollect(v.isSelected());
                if(v.isSelected()){//收藏
                    String strUrl="https://www.wanandroid.com/lg/collect/"+article.getId()+"/json";
                    new Thread(){
                        @Override
                        public void run() {
                            HttpUtil.doPost(strUrl,"");
                        }
                    }.start();

                }else {//取消收藏
                    String strUrl="https://www.wanandroid.com/lg/uncollect_originId/"+article.getId()+"/json";
                    new Thread(){
                        @Override
                        public void run() {
                            HttpUtil.doPost(strUrl,"");
                        }
                    }.start();
                }
            }
        });
        return convertView;
    }

}
